import java.io.{File, FileInputStream}
import java.text.SimpleDateFormat

import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.StringUtils

import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.Source

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}

/**
  * Created by yz on 2019/1/23
  */
object Utils {

  val isWindows = false

  def callScript(tmpDir: File, shBuffer: Seq[String]) = {
    val execCommand = new ExecCommand
    val runFile = if (Utils.isWindows) {
      new File(tmpDir, "run.bat")
    } else {
      new File(tmpDir, "run.sh")
    }
    FileUtils.writeLines(runFile, shBuffer.asJava)
    val shCommand = runFile.getAbsolutePath
    if (Utils.isWindows) {
      execCommand.exec(shCommand, tmpDir)
    } else {
      val useCommand = "chmod +x " + runFile.getAbsolutePath
      val dos2Unix = "dos2unix " + runFile.getAbsolutePath
      execCommand.exec(dos2Unix, useCommand, shCommand, tmpDir)
    }
    execCommand
  }


  def txt2Lines(file: File) = {
    FileUtils.readLines(file).asScala
  }

  def splitByTab(str:String)=str.split("\t").toBuffer

  def txt2LinesByScala(file: File) = {
    val source = Source.fromFile(file)
    try source.getLines().toBuffer finally source.close()
  }

  def lines2columns(lines: mutable.Buffer[String]) = {
    lines.map(x => x.split("\t").toBuffer)
  }

  def logTime(startTime: Long) = {
    val time = getTime(startTime)
    println(time)
  }

  def getTime(startTime: Long) = {
    val endTime = System.currentTimeMillis()
    (endTime - startTime) / 1000.0
  }

  def lines2File(file: File, lines: mutable.Buffer[String], append: Boolean = false) = {
    FileUtils.writeLines(file, lines.asJava, append)
  }

  def execFuture[T](f: Future[T]): T = {
    Await.result(f, Duration.Inf)
  }



}
